package com.turnengine.client.local.upkeep.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.upkeep.bean.IUpkeep;
import com.turnengine.client.local.upkeep.bean.UpkeepSerializer;
import java.io.IOException;

/**
 * The Add Upkeep Data Serializer.
 */
public class AddUpkeepDataSerializer extends AbstractCommandDataSerializer<IUpkeep> implements IAddUpkeepDataSerializer {

	/**
	 * Creates a new Add Upkeep Data Serializer.
	 */
	public AddUpkeepDataSerializer() {
		super(AddUpkeep.COMMAND_ID);
	}

	@Override
	public IUpkeep readReturnValue(IDataReader reader) throws IOException {
		IUpkeep value = new UpkeepSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IUpkeep value) throws IOException {
		new UpkeepSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IUpkeep> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int upkeepId = reader.readInt();
		int priority = reader.readInt();

		// Create object
		AddUpkeep object = new AddUpkeep();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setUpkeepId(upkeepId);
		object.setPriority(priority);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IUpkeep> argument) throws IOException {
		AddUpkeep object = (AddUpkeep) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int upkeepId = object.getUpkeepId();
		int priority = object.getPriority();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(upkeepId);
		writer.writeInt(priority);
	}

	@Override
	public IExecutableCommandResponse<IUpkeep> newResponse() {
		return new AddUpkeepResponse();
	}
}
